package auctions;

import java.util.Vector;

public class SequentialAuctionHouse extends AuctionHouse {

	private Vector<Integer> _schedule;
	
	public SequentialAuctionHouse(String outputFile, Vector<Agent> agents, Vector<Integer> paymentRule, Vector<Double> reserves, Vector<AuctionType> at) {
		super(outputFile, agents, paymentRule, reserves, at);
		_schedule = new Vector<Integer>();
		for (int i = 0; i<at.size();i++) {
			_schedule.add(i);
		}
	}
	
	@Override
	public AuctionHouseType getType() {
		return AuctionHouseType.SEQUENTIAL;
	}

	@Override
	public Results getWinners() {
		//TODO Report schedules to bidders
		for (int agent = 0; agent < _agents.size(); agent++) {
			_agents.get(agent).setSchedule(_schedule);
		}
		
		Vector<AuctionResults> results = new Vector<AuctionResults>();
		Vector<BidderResults> bresults = new Vector<BidderResults>();
		for (int i=0; i<_agents.size(); i++) {
			bresults.add(new BidderResults(_agents.get(i)));
		}
		
		for (int i = 0; i< _numAuctions; i++) {
			int auctionNum = _schedule.get(i);
                        _auctions.get(auctionNum).getWinner();
                        AuctionResults r;
                        if(!_auctions.get(auctionNum).getAuctionResults().isEmpty()){
                            r = _auctions.get(auctionNum).getAuctionResults().lastElement();
                            int maxBidder = r.getWinningBidder();
                            double price = r.getWinningPrice();
                            bresults.get(maxBidder).addWinning(auctionNum, price);
                        } else {
                            r= new AuctionResults(0,null,-1,auctionNum);
                        }
                        results.add(r);
			for (int agent = 0; agent < _agents.size(); agent++) {
				_agents.get(agent).submitResults(r);
			}
		}
		
		_bresults = bresults;
		AuctionsToXML writer = new AuctionsToXML(results, bresults);
		writer.generateXML(_outputFile);
        return new Results(results, bresults);
	}
	
	public void setSchedule(Vector<Integer> schedule) throws AuctionException {
		if (schedule.size() != _numAuctions) {
			throw new AuctionException("The schedule size does not match the number of auctions");
		}
		_schedule = schedule;
	}

}
